<?php

/**
 * 前台验证基类，所有前台的Controller都必须继承本类
 */
namespace Base;
use Think\Controller;

class FrontController extends Controller {

    public function _initialize() {
        $this->cur_url=$_SERVER["HTTP_HOST"].__ACTION__;
        if($_SERVER["QUERY_STRING"]){
          $output=array();
          parse_str($_SERVER["QUERY_STRING"],$output);
          unset($output['s']);
          if(count($output)){
            $query = http_build_query($output);
            $this->cur_url=$this->cur_url."?".$query;
          }
        }
        $this->cur_url='http://' .$this->cur_url;
        $suserKey=C('weixin_session_key');
        if(array_key_exists($suserKey, $_SESSION)){
          $this->weixin_user = $_SESSION[$suserKey];
        }
        else{
          if(C('wx_debug')){
            //DEBUG 写死微信用户
            $this->weixin_user =array('id'=>2,'nickname'=>'测试假微信用户','token'=>'test');
          }
          else {
            //basic house IMG server从token参数中判断用户登录与否信息
            $openid=I('urnd',false);
            //如果是测试站点，urnd写死为测试数据库中的一个固定用户
            if($_SERVER['HTTP_HOST']=="img.basichouse.aiyonet.com"){
              $openid="nPIIkBXFgBDEjSuIjv";
            }
            if($openid){
              $this->weixin_user=M('weixin_user')->where(array('token'=>$openid))->find();
            }
          }
        }
        $this->checkWxLogin();
        //获得自定义分享配置
        $this->wx_share_config();
    }

    private function wx_share_config(){
      //微信share config通过前台获得，这里不再获取了
      return;
      //DEBUG 如果是写死的微信用户，不获取自定义分享参数
      if($this->weixin_user['id']==1){
        return;
      }
      $need_weixin_authorize =C('need_weixin_authorize');
      $need_login_url = false;
      $custom_paras = null;
    $cur_url=$this->cur_url;
    if($need_weixin_authorize){
        //判断用户是否已通过微信授权
        $auth_key=C('weixin_session_key');
        session_start();
        $cuser=array();
        if(array_key_exists($auth_key,$_SESSION)){
            $cuser=$_SESSION[$auth_key];
        }
         if (stripos($cur_url,'andgoto=')>0){
            //用户虽已登录，但#未转换，则只能转换成#做一次跳转
            $cur_url=str_replace('andgoto=', '#', $cur_url);
            header('location:' . $cur_url);
            exit;
        }
  }
    //获得自定义分享参数
    $wc=array();
    try {
        $sdk = new \Home\ORG\WeixinSDK(C('APPID'),C('APPSECRET'));
        $s = new \Home\ORG\String();
        $arr = array();
        $arr['appId'] = C('APPID');
        $arr['timestamp'] = time();
        $jsapi_ticket = $sdk->getJs_ticket();
        $arr['nonceStr'] = $s->randString();
        $str = "jsapi_ticket=" . $jsapi_ticket . "&noncestr=" . $arr['nonceStr'] . "&timestamp=" . $arr['timestamp'] . "&url=" . $cur_url;
        $arr['str'] = $str;
        $arr['signature'] = sha1($str);
        $arr['cur_curl'] = $cur_url;
        $wc = $arr;
    } catch ( Exception $e ) {
            //$jumpUrl=$config['illegal_url']."?msg=".urlencode('网络原因您的微信信息暂无法获得,请稍后再试.');
            //header('location:' . $jumpUrl);
            //exit;
        }
      $this->assign('wx_share_config',$wc);
    }

    //判断用户是否微信登陆
    protected function checkWxLogin() {
      if(!$this->weixin_user){
        $redirect_url = C('login_url')."?ret_url=".urlencode($this->cur_url);
        if(IS_AJAX){
          $this->customReturn(0,array('msg'=>"您还未登录或登录信息已过期,请重新登录.",'redirect_url'=>$redirect_url));
        }
        else {
          header('location:' . $redirect_url);
        }
        exit;
      }
    }

    // public function customReturn($status,$content){
    //   $data['succ'] = $status;
    //   if(!$content){
    //     $content=array();
    //   }
    //   $data['data'] = $content;
    //   //$this->ajaxReturn($data);
    //   // echo (json_encode($data,JSON_UNESCAPED_SLASHES));
    //   echo str_replace("\\/", "/",  json_encode($data));
    //   exit;
    // }

      public function customReturn($status,$content){
      $data['succ'] = $status;
      if(!$content){
        $content=array();
      }
      $data['datas'] = $content;
      // dump($data);
      //$this->ajaxReturn($data);
      // echo (json_encode($data,JSON_UNESCAPED_SLASHES));
      echo str_replace("\\/", "/",  json_encode($data,JSON_UNESCAPED_UNICODE));
      exit;
    }

}
